package com.irdstudio.efp.console.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.console.service.dao.PubSysInfoDao;
import com.irdstudio.efp.console.service.domain.PubSysInfo;
import com.irdstudio.efp.console.service.facade.PubSysInfoService;

@Service("pubSysInfoService")
public class PubSysInfoServiceImpl implements PubSysInfoService {

	private static Logger logger = LoggerFactory.getLogger(PubSysInfoServiceImpl.class);

	@Autowired
	private PubSysInfoDao pubSysInfoDao;

	@Override
	public String getOpenday(String sysId) {
		logger.info("获取系统营业日期，系统ID：" + sysId);
		return pubSysInfoDao.getOpenday(sysId);
	}

	@Override
	public String getLastOpenday(String sysId) {
		logger.info("获取系统上一营业日期，系统ID：" + sysId);
		return pubSysInfoDao.getLastOpenday(sysId);
	}

	@Override
	/**
	 * 0:00开始日切，日终期间可以办理业务
	 */
	public void chgOpenDay(String sysId) throws Exception {
		logger.info("开始切换系统营业日期，传入系统ID：" + sysId);
		try {
			String openDay = this.getOpenday(sysId);
			String lastDay = TimeUtil.getTomorrow(openDay);// 获取明天
			PubSysInfo pubSysInfo = new PubSysInfo();
			pubSysInfo.setOpenday(lastDay);
			pubSysInfo.setLastOpenday(openDay);
			pubSysInfo.setDaybat(BaseConstant.YES);
			pubSysInfo.setSysId(sysId);
			logger.info("开始切换系统营业日期，传入参数：" + JSONObject.toJSONString(pubSysInfo));
			pubSysInfoDao.chgPubSysInfo(pubSysInfo);
			logger.info("切换系统营业日期完成!");
		} catch (Exception e) {
			logger.error("切换系统营业日期异常！", e);
			throw new Exception("日切服务异常!", e);
		}
	}

	@Override
	/**
	 * 0:00开始日切，日终期间可以办理业务
	 */
	public void chgOpenDay(String sysId, String openDay, String lastDay) throws Exception {
		logger.info("开始切换系统营业日期，传入系统ID：" + sysId);
		try {
			PubSysInfo pubSysInfo = new PubSysInfo();
			pubSysInfo.setOpenday(openDay);
			pubSysInfo.setLastOpenday(lastDay);
			pubSysInfo.setDaybat(BaseConstant.YES);
			pubSysInfo.setSysId(sysId);
			logger.info("开始切换系统营业日期，传入参数：" + JSONObject.toJSONString(pubSysInfo));
			pubSysInfoDao.chgPubSysInfo(pubSysInfo);
			logger.info("切换系统营业日期完成!");
		} catch (Exception e) {
			logger.error("切换系统营业日期异常！", e);
			throw new Exception("日切服务异常!", e);
		}
	}

	/**
	 * 日终结束后将是否日终修改为否
	 * 
	 * @return
	 */
	@Override
	public void chgDayBat(String sysId) throws Exception {
		logger.info("开始将日终标识修改,传入系统ID：" + sysId);
		try {
			PubSysInfo pubSysInfo = new PubSysInfo();
			pubSysInfo.setDaybat(BaseConstant.NO);
			pubSysInfo.setSysId(sysId);
			pubSysInfoDao.chgPubSysInfo(pubSysInfo);
			logger.info("chgDayBat传入参数：" + JSONObject.toJSONString(pubSysInfo));
			logger.info("日终结束后将是否日终修改为否完成!");
		} catch (Exception e) {
			logger.error("修改日终标识异常！", e);
			throw new Exception("修改日终标识异常!", e);
		}
	}
}
